package property.service.impl;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import property.mapper.AdminMapper;
import property.mapper.StaffMapper;
import property.mapper.UserMapper;
import property.pojo.*;

import property.service.PeopleService;
import property.utils.mapper.MyBatisUtil;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

public class PeopleServiceImpl implements PeopleService {

    @Override
    public <T> int applyRegister(T object) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        int i = 0;
        try {
            if(object.getClass().equals(User.class)){
                UserMapper mapper = sqlSession.getMapper(UserMapper.class);
                i = mapper.insertSelective((User) object);
            } else if (object.getClass().equals(Staff.class)) {
                StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
                i = mapper.insertSelective((Staff) object);
            }else {
                AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
                i = mapper.insertSelective((Admin) object);
            }
        } catch (Exception e) {
            if(e.getMessage().contains("该手机号账号已存在"))
                i = -2;
            else i = -3;
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int deregister(int type, int id){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        int i = 0;
        try {
            if(type == 0){
                UserMapper mapper = sqlSession.getMapper(UserMapper.class);
                i = mapper.deleteByPrimaryKey(id);
            } else if (type == 1) {
                StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
                i = mapper.deleteByPrimaryKey(id);
            }else {
                AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
                i = mapper.deleteByPrimaryKey(id);
            }
        }catch (Exception e){
            i = -1;
        }

        //0 -- 用户  1 -- 员工  2 -- 管理员

        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public <T> int updatePeopleData(T record) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        int i = 0;
        try{
            if(record.getClass().equals(User.class)){
                UserMapper mapper = sqlSession.getMapper(UserMapper.class);
                i = mapper.updateByPrimaryKeySelective((User) record);
            } else if (record.getClass().equals(Staff.class)) {
                StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
                i = mapper.updateByPrimaryKeySelective((Staff) record);
            } else {
                AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
                i = mapper.updateByPrimaryKeySelective((Admin) record);
            }
        }catch (Exception e){
            if(e.getMessage().contains("该手机号账号已存在"))
                i = -2;
            else i = -3;
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

}
